<template>
  <div class="personal layout-pd">
    <el-tabs
        v-model="state.radio"
        type="card"
        class="demo-tabs"
        @tab-click="handleClick"
    >
      <el-tab-pane label="全部" name="all">全部</el-tab-pane>
      <el-tab-pane label="按价格" name="price">按价格</el-tab-pane>
      <el-tab-pane label="按时间" name="time">按时间</el-tab-pane>
    </el-tabs>

    <!--    <el-radio-group v-model="state.radio" size="default" @change="onRadioChange">-->
    <!--      <el-radio-button label="all" value="all">全部</el-radio-button>-->
    <!--      <el-radio-button label="price" value="price">按价格</el-radio-button>-->
    <!--      <el-radio-button label="time" value="time">按时间</el-radio-button>-->
    <!--    </el-radio-group>-->

    <div class="flex-warp" v-if="state.tableData.goods_lists.length > 0">
      <el-row :gutter="15">
        <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="4" class="mb15" v-for="(v, k) in state.tableData.goods_lists"
                :key="k" @click="onTableItemClick(v)">
          <div class="flex-warp-item">
            <div class="flex-warp-item-box">
              <div class="item-img">
                <img :src="v.icon"/>
              </div>
              <div class="item-txt">
                <div class="item-txt-title">{{ v.name }}</div>
                <div class="item-txt-other">
                  <div style="width: 100%">
                    <div class="item-txt-msg mb10">
                      <span>评价 4.5</span>
                      <span class="ml10">收藏 2354</span>
                    </div>
                    <div class="item-txt-msg item-txt-price">
												<span class="font-price">
													<span>￥</span>
													<span class="font">{{ v.price }}</span>
												</span>
                      <span>月销3454笔</span>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </el-col>
      </el-row>
    </div>
    <el-empty v-else description="暂无数据"></el-empty>
    <template v-if="state.tableData.goods_lists.length > 0">
      <el-pagination
          style="text-align: right"
          background
          @size-change="onHandleSizeChange"
          @current-change="onHandleCurrentChange"
          :page-sizes="[12, 24, 36]"
          :current-page="state.tableData.param.pageNum"
          :page-size="state.tableData.param.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="state.tableData.total"
      >
      </el-pagination>
    </template>
  </div>
</template>

<script setup lang="ts" name="goodsLists">
import {reactive, ref} from 'vue'
import {ElMessage} from 'element-plus';
import {useRouter} from 'vue-router';
import {goodsApi} from "/@/api/goods";

const router = useRouter(); // 获取路由实例

const handleClick = (tab: TabsPaneContext, event: Event) => {
  if (tab.props.name == state.temp && state.flag) {//点击相同的选项卡时切换升序降序
    state.order = 'asc';
    state.flag = false;
  } else {
    state.flag = true;
    state.order = 'desc';
  }
  state.tableData.param.pageNum = 1;
  state.temp = tab.props.name;
  state.radio = tab.props.name;
  asyncGoods();
  // console.log(tab.props.name, event)
}
const state = reactive({
  radio: 'all',
  temp: '',
  flag: false,
  order: 'asc',
  tableData: {
    goods_lists: [],
    total: 0,
    loading: false,
    param: {
      pageNum: 1,
      pageSize: 12,
    },
  },
});
// 单选框改变时
// const onRadioChange = () => {
//   console.log(state.radio,111)
// };

const asyncGoods = async () => {
  const str = state.radio + ',' + state.order;
  const res = await goodsApi().lists({
    page: state.tableData.param.pageNum,
    size: state.tableData.param.pageSize,
    order_key_str: str
  })
  if (res.code != 0) {
    ElMessage.error(res.msg)
    return;
  }
  if (!res.data) return;
  state.tableData.goods_lists = [];
  state.tableData.total = res.data.count;
  res.data.list.map((item) => {
    state.tableData.goods_lists.push(item)
  });
}
asyncGoods();

// 当前列表项点击
const onTableItemClick = (v: FilterListType) => {
  router.push({
    path: '/goods_detail',
    query: { id: v.id },
  });
};
// 分页点击
const onHandleSizeChange = (val: number) => {
  state.tableData.param.pageSize = val;
  state.tableData.param.pageNum = 1;
  asyncGoods();
};
// 分页点击
const onHandleCurrentChange = (val: number) => {
  state.tableData.param.pageNum = val;
  asyncGoods();
};
</script>

<style scoped lang="scss">
@import '../../../theme/mixins/index.scss';

.personal {
  text-align: left;
}

.demo-tabs {
  :deep(.el-tabs__header) {
    border-bottom: none !important;
  }

  :deep(.el-tabs__nav) {
    border-bottom: 1px solid var(--el-border-color-light);
  }

  :deep(.el-tabs__content) {
    display: none;
  }
}

.flex-warp {
  display: flex;
  flex-wrap: wrap;
  align-content: flex-start;
  margin: 20px -5px 0px -5px;

  .el-row {
    width: 101%;
  }

  .flex-warp-item {
    padding: 5px;
    width: 100%;
    height: 360px;

    .flex-warp-item-box {
      border: 1px solid var(--next-border-color-light);
      width: 100%;
      height: 100%;
      border-radius: 2px;
      display: flex;
      flex-direction: column;
      transition: all 0.3s ease;

      &:hover {
        cursor: pointer;
        border: 1px solid var(--el-color-primary);
        transition: all 0.3s ease;
        box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.03);

        .item-txt-title {
          color: var(--el-color-primary) !important;
          transition: all 0.3s ease;
        }

        .item-img {
          img {
            transition: all 0.3s ease;
            transform: translateZ(0) scale(1.05);
          }
        }
      }

      .item-img {
        width: 100%;
        height: 215px;
        overflow: hidden;

        img {
          transition: all 0.3s ease;
          width: 100%;
          height: 100%;
        }
      }

      .item-txt {
        flex: 1;
        padding: 15px;
        display: flex;
        flex-direction: column;
        overflow: hidden;

        .item-txt-title {
          text-overflow: ellipsis;
          overflow: hidden;
          -webkit-line-clamp: 2;
          -webkit-box-orient: vertical;
          display: -webkit-box;
          color: #666666;
          transition: all 0.3s ease;

          &:hover {
            color: var(--el-color-primary);
            text-decoration: underline;
            transition: all 0.3s ease;
          }
        }

        .item-txt-other {
          flex: 1;
          align-items: flex-end;
          display: flex;

          .item-txt-msg {
            font-size: 12px;
            color: #8d8d91;
          }

          .item-txt-price {
            display: flex;
            justify-content: space-between;
            align-items: center;

            .font-price {
              color: #ff5000;

              .font {
                font-size: 22px;
              }
            }
          }
        }
      }
    }
  }
}
</style>
